package com.lml.interviewTest;

import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2021/6/1 14:45
 * Description:
 */
public class Test01 {
    public static void main(String[] args) {
        String s1 = "Programming";
        String s2 = new String("Programming");
        String s3 = "Program";
        String s4 = "ming";
        String s5 = "Program" + "ming";
        String s6 = s3 + s4;
        System.out.println(s1 == s2);
        System.out.println(s1 == s5);
        System.out.println(s1 == s6);
        System.out.println(s1 == s6.intern());
        System.out.println(s2 == s2.intern());
        System.out.println(s5 == s6);
        System.out.println("------------------------");
        //58面试题
        String s7 = new StringBuffer("58").append("tongcheng").toString();
        System.out.println(s7 == s7.intern());

        String s8 = new StringBuffer("ja").append("va").toString();
        System.out.println(s8 == s8.intern());
    }

    @Test
    public void test01() {
        //如何取得年月日、小时分钟秒？
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.get(Calendar.YEAR));
        System.out.println(cal.get(Calendar.MONTH)); // 0 - 11
        System.out.println(cal.get(Calendar.DATE));
        System.out.println(cal.get(Calendar.HOUR_OF_DAY));
        System.out.println(cal.get(Calendar.MINUTE));
        System.out.println(cal.get(Calendar.SECOND));
        // Java 8
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(dt.getYear());
        System.out.println(dt.getMonthValue()); // 1 - 12
        System.out.println(dt.getDayOfMonth());
        System.out.println(dt.getHour());
        System.out.println(dt.getMinute());
        System.out.println(dt.getSecond());
    }

    @Test
    public void test02() {
        //如何取得从 1970 年 1 月 1 日 0 时 0 分 0 秒到现在的毫秒数？
        Calendar.getInstance().getTimeInMillis(); //第一种方式
        System.currentTimeMillis(); //第二种方式
        // Java 8
        Clock.systemDefaultZone().millis();
    }

    @Test
    public void test03() {
        //如何取得某月的最后一天？
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为 1 号,当前日期既为本月第一天

//
//        String first = format.format(c.getTime());
//        System.out.println("===============first:"+first);
//
//        //获取当前月最后一天
//        Calendar ca = Calendar.getInstance();
//        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
//        String last = format.format(ca.getTime());
//        System.out.println("===============last:"+last);

        //Java 8
        LocalDate today = LocalDate.now();
        //本月的第一天
        LocalDate firstday = LocalDate.of(today.getYear(), today.getMonth(), 1);
        //本月的最后一天
        LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("本月的第一天" + firstday);
        System.out.println("本月的最后一天" + lastDay);
    }

    @Test
    public void test04() {
        //如何格式化日期？
        SimpleDateFormat oldFormatter = new SimpleDateFormat("yyyy/MM/dd");
        Date date1 = new Date();
        System.out.println(oldFormatter.format(date1));

        // Java 8
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate date2 = LocalDate.now();
        System.out.println(date2.format(newFormatter));
    }

    @Test
    public void test05() {
        //打印昨天的当前时刻？
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        System.out.println(cal.getTime());

        //java8
        LocalDateTime today = LocalDateTime.now();
        LocalDateTime yesterday = today.minusDays(1);
        System.out.println(yesterday);
    }

    @Test
    public void test06() {
        //用面向对象的方法求出数组中重复 value 的个数
        int[] arr = {1, 4, 1, 4, 2, 5, 4, 5, 8, 7, 8, 77, 88, 5, 4, 9, 6, 2, 4, 1, 5};
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : arr) {
            if (map.containsKey(i)) {
                map.put(i, map.get(i) + 1);
            } else {
                map.put(i, 1);
            }
        }
        map.forEach((x, y) -> System.out.println(x + "出现的次数：" + y));
    }

    @Test
    public void test07() {
        System.out.println(get2n(7));
        System.out.println(minN(127));
    }

    /**
     * 给定一个数，求大于该数的最小2的n次幂，返回n
     *
     * @return n
     */
    public static int minN(int x) {
        int n = 0;
        while (get2n(n) <= x) {
            n++;
        }
        return n;
    }

    public static int get2n(int n) {
        int num = 1;
        while ((n--) != 0) {
            num *= 2;
        }
        return num;
    }

    @Test
    public void test08() {
        StringBuffer a = new StringBuffer("A");
        StringBuffer b = new StringBuffer("B");
        operator(a, b);
        System.out.println(a + "," + b);
    }

    public static void operator(StringBuffer x, StringBuffer y) {
        x.append(y);
        y = x;
    }

    @Test
    public void test09() {
        int x = 0;
        int y = 10;
        do {
            y--;
            ++x;
        } while (x < 6);
        System.out.println(x + ", " + y);
    }

    @Test
    public void test10() {
        //thread的start和run的区别
        //https://zhidao.baidu.com/question/1367708654192998379.html
        Thread t = new Thread() {
            public void run() {
                System.out.println("pong");
            }
        };
        t.run();
        System.out.print("ping");
    }

    @Test
    public void test11() {
        System.out.println(Integer.MAX_VALUE * 2);
        System.out.println(Integer.MIN_VALUE * 2);
    }


    @Test
    public void test12() {
        //输出结果是什么？
        try {
            testException(new int[]{0, 1, 2, 3, 4, 5});
        } catch (Exception e) {
            System.out.println("E");
        }
    }

    public static void testException(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            try {
                if (arr[i] % 2 == 0) {
                    throw new NullPointerException();
                } else {
                    System.out.println(i);
                }
            } finally {
                System.out.println("e");
            }
        }
    }

    @Test
    public void test13() {

    }
}
